package com.timothy.android.answer;

import java.util.ArrayList;
import java.util.List;

import com.timothy.android.BaseAnswerActivity;
import com.timothy.android.util.NumberUtil;
import com.timothy.android.util.ValueCopyUtil;
import com.timothy.android.util.XMLUtil;
import com.timothy.chart.BaseChart;
import com.timothy.chart.CanvasSetting;
import com.timothy.chart.Bar2D;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class Bar2DAnswerActivity extends BaseAnswerActivity {
	private final static String LOG_TAG = "Bar2DAnswerActivity";
	List<Bar2D> barList = new ArrayList<Bar2D>();
	SurfaceView view = null;
	
	public void setView(int orientation){
		switch(orientation){
			case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:view = new VerticalBar2DView(this);break;
			case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:view = new HorizontalBar2DView(this);break;
			default:view = new VerticalBar2DView(this);
		}
	}
	
/*	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.i(LOG_TAG,"onConfigurationChanged:orientation="+newConfig.orientation);
		super.onConfigurationChanged(newConfig);
		Intent intent = new Intent();
		intent.putExtra("message", dataXML);
		intent.setClass( getBaseContext(), Bar2DAnswerActivity.class);
		startActivity(intent);
	}*/
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.i(LOG_TAG,"onCreate");
//		this.setTitle("Bar2DAnswer");
		this.appendTitle("-Bar2DAnswer");
		
		Bundle bundle = this.getIntent().getExtras();
		String displayStyle = bundle.getString("displayStyle");
		
		//get answer data list
		for(int i=0;i<chartList.size();i++){
			Log.i(LOG_TAG,"Loop chartList for PieList");
			
			BaseChart baseChart = chartList.get(i);
			Log.i(LOG_TAG,baseChart.toString());
			
			Bar2D bar2D = new Bar2D();
			try {
				ValueCopyUtil.valueCopy(baseChart, bar2D);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Log.i(LOG_TAG,bar2D.toString());
			barList.add(bar2D);
		}
		
		if("1".equals(displayStyle)){
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			setView(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);			
		}else if("2".equals(displayStyle)){
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			setView(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);			
		}else{
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			setView(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);			
		}

		view.setOnTouchListener(this);
		view.setLongClickable(true);
		mGestureDetector = new GestureDetector((OnGestureListener) this); 
		
		setContentView(view);
		

	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}
	
	class VerticalBar2DView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;
		private final static float BAR_RATIO = 0.8f;

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public VerticalBar2DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "VerticalBar2DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "VerticalBar2DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalBar2DView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalBar2DView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalBar2DView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalBar2DView:bottomM:"+bottomM);
			
			//set canvas background color
			canvas.drawColor(setting.getBgColor());// 设置画布背景为黑色
			
			Paint mPaint = new Paint();
			

			//write question
			
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalBar2DView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalBar2DView:queY:"+queY);
			canvas.drawText(baseBean.getQuestionText(),queX,queY/2,mPaint);
			canvas.save();
			
//			Log.i(LOG_TAG, "VerticalBar2DView:spaceInCharts:"+setting.getSpace());
			int canvasNoMarginWidth = canvasWidth - setting.getLeftMargin() - setting.getRightMargin();
			double barWidthInclSpace = (canvasNoMarginWidth * 1.0)/barList.size();
			int barWidth = (int)(barWidthInclSpace*0.9);
			int spaceWidth = (int)(barWidthInclSpace*0.1);
			if(spaceWidth<1) spaceWidth = 1;
			Log.i(LOG_TAG, "VerticalBar2DView:barWidth:"+barWidth);
			Log.i(LOG_TAG, "VerticalBar2DView:spaceWidth:"+spaceWidth);
			
			setting.setSpace(spaceWidth);
			
			int canvasNoMarginHeight = canvasHeight-setting.getTopMargin()-setting.getBottomMargin();
			
			for(int i=0;i<barList.size();i++){
				Bar2D bar2D = barList.get(i);
				
				//根据总票数，计算出比例，再乘以画布的高度
				int votes = bar2D.getVotes();
				double votesShare = bar2D.getShare();
				Log.i(LOG_TAG, "VerticalBar2DView:votes:"+votes);
				Log.i(LOG_TAG, "VerticalBar2DView:votesShare:"+votesShare);
				
				int barHeigth = (int)((votesShare * canvasNoMarginHeight)*BAR_RATIO);
				Log.i(LOG_TAG, "VerticalBar2DView:barHeigth:"+barHeigth);
				
				int tlX = (int) (setting.getLeftMargin()+ i * (barWidth + setting.getSpace()));
				int tlY =  canvasHeight - setting.getBottomMargin() - barHeigth;
				int brX = tlX + barWidth;
				int brY = canvasHeight - setting.getBottomMargin();
				
				//draw bar
				Log.i(LOG_TAG, "VerticalBar2DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
				Log.i(LOG_TAG, "VerticalBar2DView:fullColor="+bar2D.getFullColor());
				mPaint.setColor(Color.parseColor(bar2D.getFullColor()));
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				canvas.save();
				
				//draw choice text
				Log.i(LOG_TAG, "VerticalBar2DView:ChoiceTextSize="+bar2D.getChoiceTextSize());
				Log.i(LOG_TAG, "VerticalBar2DView:ChoiceTextColor="+bar2D.getChoiceTextColor());
				Log.i(LOG_TAG, "VerticalBar2DView:ChoiceText="+bar2D.getChoiceText());
				mPaint.setTextSize(bar2D.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(bar2D.getChoiceTextColor()));
				canvas.drawText(bar2D.getChoiceText(), tlX, canvasHeight-setting.getBottomMargin()/2, mPaint);
				canvas.save();
				
				//draw votes and share
				Log.i(LOG_TAG, "VerticalBar2DView:AnswerTextSize="+bar2D.getAnswerTextSize());
				Log.i(LOG_TAG, "VerticalBar2DView:AnswerTextColor="+bar2D.getAnswerTextColor());
				mPaint.setTextSize(bar2D.getAnswerTextSize());
				mPaint.setColor(Color.parseColor(bar2D.getAnswerTextColor()));
				canvas.drawText(String.valueOf(bar2D.getVotes()), tlX, tlY-20, mPaint);
				String formatShare = "0%";
				if(bar2D.getShare()*100>0){
					formatShare = NumberUtil.formatData1Deci(bar2D.getShare()*100)+"%";
				}
				canvas.drawText(formatShare, tlX, tlY-40, mPaint);
				canvas.save();
				
				//draw border
				mPaint.setColor(Color.WHITE);
				for(int x=1;x<10;x++){
					//draw horizontal lines
					canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
				}
				
				for(int y=1;y<10;y++){
					//draw vertical lines
					canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
				}
				canvas.save();
				
			}

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			
		}
	}
	
	class HorizontalBar2DView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;
		private final static float BAR_RATIO = 0.8f;

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			recoverBrightness();
			return super.onTouchEvent(event);
		}
		
		public HorizontalBar2DView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();
			
			int canvasHeight = canvas.getHeight();
			int canvasWidth = canvas.getWidth();
			Log.i(LOG_TAG, "HorizontalBar2DView:Canvas Height:"+canvasHeight);
			Log.i(LOG_TAG, "HorizontalBar2DView:Canvas Width:"+canvasWidth);
			
			CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,2);
			
			int leftM = setting.getLeftMargin();
			int rightM = setting.getRightMargin();
			int topM = setting.getTopMargin();
			int bottomM = setting.getBottomMargin();
			
			Log.i(LOG_TAG, "VerticalBar2DView:leftM:"+leftM);
			Log.i(LOG_TAG, "VerticalBar2DView:rightM:"+rightM);
			Log.i(LOG_TAG, "VerticalBar2DView:topM:"+topM);
			Log.i(LOG_TAG, "VerticalBar2DView:bottomM:"+bottomM);
			
			//write question
			Paint mPaint = new Paint();
			mPaint.setColor(Color.parseColor(questionTextColor));		
			mPaint.setTextSize(textSize);
			int queX = setting.getLeftMargin();
			int queY = setting.getTopMargin();
			Log.i(LOG_TAG, "VerticalBar2DView:queX:"+queX);
			Log.i(LOG_TAG, "VerticalBar2DView:queY:"+queY);
			canvas.drawText(answer.getQuestion().getQuestionName(),queX,queY/2,mPaint); 
			canvas.save();
			
			//根据图形个数既图形间距计算出图形高度
			Log.i(LOG_TAG, "HorizontalBar2DView:spaceInCharts:"+setting.getSpace());
			
			int canvasNoMarginHeight = canvasHeight - setting.getTopMargin() - setting.getBottomMargin();
			double barHeightInclSpace = (canvasNoMarginHeight * 1.0)/barList.size();
			int barHeight = (int)(barHeightInclSpace*0.9);
			int spaceHeight = (int)(barHeightInclSpace*0.1);
			if(spaceHeight<1) spaceHeight = 1;
			
			int canvasNoMarginWidth = canvasWidth-setting.getLeftMargin()-setting.getRightMargin();
			
			for(int i=0;i<barList.size();i++){
				Bar2D bar2D = barList.get(i);
				
				//根据总票数，计算出比例，再乘以画布的高度
				
				int votes = bar2D.getVotes();
				double votesShare = bar2D.getShare();
				int barWidth = (int) (votesShare * canvasNoMarginWidth * BAR_RATIO);
				
				Log.i(LOG_TAG, "HorizontalBar2DView:barWidth:"+barWidth);
				Log.i(LOG_TAG, "HorizontalBar2DView:votes:"+votes);
				Log.i(LOG_TAG, "HorizontalBar2DView:votesShare:"+votesShare);
				
				int tlX = setting.getLeftMargin();
				int tlY = setting.getTopMargin()+ (int)(i*(barHeight + spaceHeight));
				int brX = tlX + barWidth;
				int brY = tlY + barHeight;
				
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
				mPaint.setColor(Color.parseColor(bar2D.getFullColor()));
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
				canvas.save();
				
				//draw answer text
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX:AnswerTextSize="+bar2D.getAnswerTextSize());
				Log.i(LOG_TAG, "HorizontalBar2DView:tlX:AnswerTextColor="+bar2D.getAnswerTextColor());
				mPaint.setTextSize(bar2D.getAnswerTextSize());
				mPaint.setColor(Color.parseColor(bar2D.getAnswerTextColor()));
				String formatAnswer = "0 0%";
				if(bar2D.getVotes()>0){
					formatAnswer = bar2D.getVotes() + " " + NumberUtil.formatData1Deci(bar2D.getShare()*100)+"%";
				}
				canvas.drawText(formatAnswer, tlX + barWidth+leftM/4, tlY+barHeight/2, mPaint);
				canvas.save();
				
				//draw choice
				Log.i(LOG_TAG, "VerticalBar2DView:ChoiceTextSize="+bar2D.getChoiceTextSize());
				Log.i(LOG_TAG, "VerticalBar2DView:ChoiceTextColor="+bar2D.getChoiceTextColor());
				mPaint.setTextSize(bar2D.getChoiceTextSize());
				mPaint.setColor(Color.parseColor(bar2D.getChoiceTextColor()));

				canvas.drawText(bar2D.getChoiceText(),leftM/4, tlY+barHeight/2, mPaint);
				canvas.save();
			}
			
			//draw border
			mPaint.setColor(Color.WHITE);
			for(int x=1;x<10;x++){
				//draw horizontal lines
				canvas.drawLine(leftM,x* canvasHeight/10, canvasWidth-rightM,x*canvasHeight/10, mPaint);
			}
			for(int y=1;y<10;y++){
				//draw vertical lines
				canvas.drawLine(y* canvasWidth/10,topM, y* canvasWidth/10, canvasHeight-bottomM, mPaint);
			}
			canvas.save();

			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			
		}
	}
	
}
